package com.redpill.linpro.transaction;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import com.redpill.linpro.transaction.impl.TransactionServiceNull;

@Aspect
public class TransactionAspect {
	
	private TransactionService transactionService = new TransactionServiceNull();	
	

	@Pointcut("execution(@org.springframework.transaction.annotation.Transactional * *(..))")
	public void transactionalMethod() {}

	
	@Before("transactionalMethod()")
	public void beforeTransactionalMethod(JoinPoint joinPoint) {
		transactionService.beginTransaction();
	}
	
	@AfterReturning("transactionalMethod()")
	public void afterTransactionalMethod(JoinPoint joinPoint) {
		transactionService.commit();
	}
	
	@AfterThrowing(pointcut = "transactionalMethod()")
	public void afterThrowingFromTransactionalMethod(JoinPoint joinPoint) {
		transactionService.rollback();
	}
	
	public void setTransactionService(final TransactionService transactionService) {
		this.transactionService = transactionService;
	}
}

